package com.sh.rbac.controller;

import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.job.JobQueryDTO;
import com.sh.rbac.domain.dto.job.JobSaveDTO;
import com.sh.rbac.domain.dto.job.JobUpdateDTO;
import com.sh.rbac.domain.entity.Job;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.JobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务控制器
 *
 * @author wb
 * @since 2024-08-04
 */
@Tag(name = "任务管理", description = "任务相关操作")
@RestController
@RequestMapping("monitor/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加任务", description = "保存一个任务")
    @ApiResponse(responseCode = "200", description = "成功保存任务")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('monitor:job:save')")
    public Result<Object> save(@RequestBody JobSaveDTO jobSaveDTO) {
        return jobService.save(jobSaveDTO) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "执行一次任务", description = "执行一次任务")
    @ApiResponse(responseCode = "200", description = "成功执行任务")
    @PutMapping("run/{id}")
    @PreAuthorize("hasAuthority('monitor:job:run')")
    public Result<Boolean> runJob(@PathVariable Long id) {
        return jobService.runJob(id) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "启动任务", description = "启动一个任务")
    @ApiResponse(responseCode = "200", description = "成功启动任务")
    @PutMapping("start/{id}")
    @PreAuthorize("hasAuthority('monitor:job:start')")
    public Result<Boolean> startJob(@PathVariable Long id) {
        return jobService.startJob(id) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "暂停任务", description = "暂停一个任务")
    @ApiResponse(responseCode = "200", description = "成功暂停任务")
    @PutMapping("pause/{id}")
    @PreAuthorize("hasAuthority('monitor:job:pause')")
    public Result<Boolean> pauseJob(@PathVariable Long id) {
        return jobService.pauseJob(id) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新任务", description = "根据ID更新任务信息")
    @ApiResponse(responseCode = "200", description = "成功更新任务")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('monitor:job:update')")
    public Result<Object> update(@RequestBody JobUpdateDTO jobUpdateDTO) {
        return Result.success(jobService.updateById(jobUpdateDTO));
    }


    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除任务", description = "删除一个任务")
    @ApiResponse(responseCode = "200", description = "成功删除任务")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('monitor:job:delete')")
    public Result<Object> delete(@PathVariable Long id) {
        return jobService.removeById(id) ? Result.success() : Result.failure();
    }

    @Operation(summary = "查询单个任务")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Job.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('monitor:job:get')")
    public Result<Object> get(@PathVariable Long id) {
        return Result.success(jobService.getById(id));
    }

    @Operation(summary = "分页查询所有任务")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Job.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("hasAuthority('monitor:job:page')")
    public Result<Object> list(@PathVariable Long current, @PathVariable Long limit, @RequestBody JobQueryDTO jobQueryDTO) {
        return Result.success(jobService.getPage(current, limit, jobQueryDTO));
    }

    @OperationLogger(module = "系统管理->任务管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除任务", description = "批量删除任务信息")
    @ApiResponse(responseCode = "200", description = "成功删除任务")
    @DeleteMapping("batch-delete")
    @PreAuthorize("hasAuthority('monitor:job:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return jobService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

}
